package gijoe.controller;

import gijoe.dto.ApplicationDto;
import gijoe.dto.BoardNoticeDto;
import gijoe.dto.BoardRecruitDto;
import gijoe.dto.ChartDto;
import gijoe.dto.CommcdDto;
import gijoe.service.AdminService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AdminController {
  
  private AdminService service;
  
  @Autowired
  public void setService(AdminService service) {
    this.service = service;
  }
  
  /* admin 페이지 로딩 : 최영주----------------------------------------------------------- */
  @RequestMapping("/admin_login.do")
  public ModelAndView admin_login() {
    return new ModelAndView("admin.inc.admin_login");
  }
  
  /* 공지사항 START:손정원------------------------------------------------------------------ */
  
  // 공지사항 리스트 paremeter(x) return(list) method(noticeSelectAll)
  @RequestMapping("/admin_notice_list.do")
  public ModelAndView admin_notice_list(HttpServletRequest request) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getNoticeCount();
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
    
    ModelAndView mvc = new ModelAndView();
    
    HashMap map = new HashMap();
    
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    mvc.setViewName("admin.inc.admin_notice_list");
    mvc.addObject("map", map);
    List<BoardNoticeDto> list = service.noticeSelectAll(map);
    System.out.println(map);
    mvc.addObject("list", list);
    
    /*
     * request.setAttribute("pg",pg); request.setAttribute("allPage",allPage); request.setAttribute("block",block); request.setAttribute("fromPage",fromPage); request.setAttribute("toPage",toPage);
     */
    
    return mvc;
  }
  
  // 게시판검색
  @RequestMapping("/notice_search.do")
  public ModelAndView notice_search(HttpServletRequest request, String search) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getNoticeSearch(search);
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
    
    ModelAndView mvc = new ModelAndView();
    
    HashMap map = new HashMap();
    map.put("search", search);
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    mvc.setViewName("admin.inc.admin_notice_search");
    mvc.addObject("map", map);
    List<BoardNoticeDto> list = service.noticeSearch(map);
    System.out.println(map);
    mvc.addObject("list", list);
    
    /*
     * request.setAttribute("pg",pg); request.setAttribute("allPage",allPage); request.setAttribute("block",block); request.setAttribute("fromPage",fromPage); request.setAttribute("toPage",toPage);
     */
    
    return mvc;
    
  }
  
  // 공지사항 상세 parameter(notice_num) return(dto) method(admin_notice_detail)
  @RequestMapping("/admin_notice_detail.do")
  public ModelAndView admin_notice_detail(@RequestParam String notice_num) {
    return new ModelAndView("admin.inc.admin_notice_detail", "dto", service.noticeSelectOne(notice_num));
  }
  
  // 공지사항 등록 폼 (페이지로딩)
  @RequestMapping("/admin_notice_form.do")
  public ModelAndView admin_notice_form() {
    return new ModelAndView("admin.inc.admin_notice_form");
  }
  
  // 공지사항 등록 => 리스트 페이지로 이동
  @RequestMapping("/admin_notice_write.do")
  public ModelAndView admin_notice_write(BoardNoticeDto dto) {
    System.out.println(dto.getNotice_nm() + "/" + dto.getNotice());
    service.noticeInsert(dto);
    return new ModelAndView("redirect:admin_notice_list.do");
  }
  
  // 공지사항 삭제 (detail에서 삭제를 할 수 있음)
  @RequestMapping("/admin_notice_delete.do")
  public ModelAndView admin_notice_delete(BoardNoticeDto dto) {
    System.out.println(dto.getNotice_num());
    service.noticeDelete(dto);
    return new ModelAndView("redirect:admin_notice_list.do");
  }
  
  // 공지사항 수정 폼
  @RequestMapping("/admin_notice_update.do")
  public ModelAndView admin_notice_update(@RequestParam String notice_num) {
    return new ModelAndView("admin.inc.admin_notice_update", "dto", service.noticeSelectOne(notice_num));
  }
  
  // 공지사항 수정
  @RequestMapping("/admin_notice_update_ok.do")
  public ModelAndView admin_notice_update_ok(BoardNoticeDto dto) {
    System.out.println(dto.toString());
    service.noticeUpdate(dto);
    System.out.println("updating~~~~~~~~~업데이트중!");
    return new ModelAndView("redirect:admin_notice_list.do");
  }
  
  /* 공지사항 END:손정원------------------------------------------------------------------ */
  
  /* 채용공고 START:손정원------------------------------------------------------------------ */
  
  // 채용공고 리스트
  @RequestMapping("/admin_recruit_list.do")
  public ModelAndView admin_recruit_list(HttpServletRequest request) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getRecruitCount();
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
    
    ModelAndView mvc = new ModelAndView();
    
    HashMap map = new HashMap();
    
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    mvc.setViewName("admin.inc.admin_recruit_list");
    mvc.addObject("map", map);
    List<BoardRecruitDto> list = service.recruitSelectAll(map);
    System.out.println(map);
    mvc.addObject("list", list);
    
    /*
     * request.setAttribute("pg",pg); request.setAttribute("allPage",allPage); request.setAttribute("block",block); request.setAttribute("fromPage",fromPage); request.setAttribute("toPage",toPage);
     */
    
    return mvc;
  }
  
  // 게시판검색
  @RequestMapping("/recruit_search.do")
  public ModelAndView recruit_search(HttpServletRequest request, String search) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getRecruitSearch(search);
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
    
    ModelAndView mvc = new ModelAndView();
    
    HashMap map = new HashMap();
    map.put("search", search);
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    mvc.setViewName("admin.inc.admin_recruit_search");
    mvc.addObject("map", map);
    List<BoardRecruitDto> list = service.recruitSearch(map);
    System.out.println(map);
    mvc.addObject("list", list);
    
    /*
     * request.setAttribute("pg",pg); request.setAttribute("allPage",allPage); request.setAttribute("block",block); request.setAttribute("fromPage",fromPage); request.setAttribute("toPage",toPage);
     */
    
    return mvc;
    
  }
  
  // 채용공고 상세 
  @RequestMapping("/admin_recruit_detail.do")
  public ModelAndView admin_recruit_detail(@RequestParam String rcrt_num) {
    return new ModelAndView("admin.inc.admin_recruit_detail", "dto", service.recruitSelectOne(rcrt_num));
  }
  
  // 채용공고 등록 (채용 공고 리스트 페이지로 감)
  @RequestMapping("/admin_recruit_write.do")
  public ModelAndView admin_recruit_write(BoardRecruitDto dto) {
    System.out.println(dto.toString());
    service.recruitInsert(dto);
    return new ModelAndView("redirect:admin_recruit_list.do");
  }
  
  // 채용공고 등록 폼 (페이지로딩)
  @RequestMapping("/admin_recruit_form.do")
  public ModelAndView admin_recruit_form() {
    return new ModelAndView("admin.inc.admin_recruit_form", "list", service.recruit_cdSelect());
  }
  
  // 채용공고 삭제 (detail에서 삭제를 할 수 있음)
  @RequestMapping("/admin_recruit_delete.do")
  public ModelAndView admin_recruit_delete(BoardRecruitDto dto) {
    service.recruitDelete(dto);
    return new ModelAndView("redirect:admin_recruit_list.do");
  }
  
  // 채용공고 수정
  
  @RequestMapping("/admin_recruit_update.do")
  public ModelAndView admin_recruit_update(@RequestParam String rcrt_num) {
    
    ModelAndView mvc = new ModelAndView();
    
    List<CommcdDto> cdlist = service.recruit_cdSelect();
    BoardRecruitDto rcrt_list = service.recruitSelectOne(rcrt_num);
    mvc.addObject("cdlist", cdlist);
    mvc.addObject("rcrt_list", rcrt_list);
    
    mvc.setViewName("admin.inc.admin_recruit_update");
    return mvc; 
  }
  
  @RequestMapping("/admin_recruit_update_ok.do")
  public ModelAndView admin_recruit_update_ok(BoardRecruitDto dto) {

    service.recruitUpdate(dto);

    return new ModelAndView("redirect:admin_recruit_list.do");
  }
  
  /* 채용공고 END:손정원------------------------------------------------------------------ */
  
  /* 지원자조회 및 평가 START : 최영주------------------------------------------------------ */
  // 지원자조회 
  @RequestMapping("/admin_app_list.do")
  public ModelAndView admin_app_list(HttpServletRequest request) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getAppCount();
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
    
    ModelAndView mvc = new ModelAndView();
    
    HashMap map = new HashMap();
    
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    
    System.out.println(map);
    
    mvc.setViewName("admin.inc.admin_app_list");
    mvc.addObject("map", map);
    
    List<ApplicationDto> list = service.appSelectAll(map); 
    mvc.addObject("list", list);
    
    List<BoardRecruitDto> rcrt_list = service.recruitSelectAllOption();
    mvc.addObject("rcrt_list", rcrt_list);
    
    /*
     * request.setAttribute("pg",pg); request.setAttribute("allPage",allPage); request.setAttribute("block",block); request.setAttribute("fromPage",fromPage); request.setAttribute("toPage",toPage);
     */
    
    return mvc;
  }

  // 게시판검색
  @RequestMapping("/app_search.do")
  public ModelAndView app_search(HttpServletRequest request, String app_nm, String app_stat, String rcrt_num) {
    int pg = 1;
    String strPg = request.getParameter("pg");
    if (strPg != null) {
      pg = Integer.parseInt(strPg);
    }
    int rowSize = 10;
    int start = (pg * rowSize) - (rowSize - 1);
    int end = pg * rowSize;
    
    int total = service.getAppSearch(app_nm, app_stat, rcrt_num);
    System.out.println("시작 : " + start + " 끝:" + end);
    System.out.println("글의 수 : " + total);
    
    int allPage = (int) Math.ceil(total / (double) rowSize); // 페이지수
    // int totalPage = total/rowSize + (total%rowSize==0?0:1);
    System.out.println("페이지수 : " + allPage);
    
    int block = 5; // 한페이지에 보여줄 범위 << [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] >>
    int fromPage = ((pg - 1) / block * block) + 1; // 보여줄 페이지의 시작
    // ((1-1)/10*10)
    int toPage = ((pg - 1) / block * block) + block; // 보여줄 페이지의 끝
    if (toPage > allPage) { // 예) 20>17
      toPage = allPage;
    }
   
    HashMap map = new HashMap();
    map.put("app_nm", app_nm);
    map.put("app_stat", app_stat);
    map.put("rcrt_num", rcrt_num);
    map.put("total", total);
    map.put("start", start);
    map.put("end", end);
    map.put("pg", pg);
    map.put("allPage", allPage);
    map.put("block", block);
    map.put("fromPage", fromPage);
    map.put("toPage", toPage);
    
    System.out.println(map);

    List<ApplicationDto> list = service.appSearch(map); 
    List<BoardRecruitDto> rcrt_list = service.recruitSelectAllOption();
    
    ModelAndView mvc = new ModelAndView();
    
    mvc.setViewName("admin.inc.admin_app_search");
    mvc.addObject("map", map);
    mvc.addObject("list", list);
    mvc.addObject("rcrt_list", rcrt_list);
    
    return mvc; 
  }
  
  // 지원자상세
  @RequestMapping("/admin_app_detail.do")
  public ModelAndView admin_app_detail(@RequestParam String app_num) {
    System.out.println(service.appSelectOne(app_num));
    return new ModelAndView("admin.inc.admin_app_detail", "application", service.appSelectOne(app_num));
  }
 
  //지원자평가
  @RequestMapping("/admin_evaluation.do")
  public ModelAndView admin_evaluation(@RequestParam String pass_list) {
    System.out.println("pass_list" + pass_list);
    service.passUpdate(pass_list);
    return new ModelAndView("redirect:admin_app_list.do");
  }
  
  
  
  /* 지원자조회 및 평가 END : 최영주-------------------------------------------------------- */
  
  /* 통계 START : 김윤철--------------------------------------------------------------------- */

	@RequestMapping("/admin_statistics.do")
	public ModelAndView admin_statistics() {
		return new ModelAndView("admin.inc.admin_statistics");
	}
	
	//신입/경력
	@RequestMapping("/admin_chart/admin_Chart_Basicbar.do")
	public @ResponseBody Map<String, Object> chart() {

		Map<String, Object> hasmap = new HashMap<String, Object>();
		List<ChartDto> list = null;
		List<ChartDto> list2 = null;
		list = service.Chart_recruit_p();
		list2 = service.Chart_recruit_n(); 
		hasmap.put("recruit_p", list);
		hasmap.put("recruit_n", list2);
		
		return hasmap;

	}
	
	//성별 남자 여자
	@RequestMapping("/admin_chart/admin_Chart_Basicline.do")
	public @ResponseBody Map<String, Object> chart2() {

		Map<String, Object> hasmap = new HashMap<String, Object>();
		List<ChartDto> list = null;
		List<ChartDto> list2 = null;
		list = service.Chart_sex_man();
		list2 = service.Chart_sex_she(); 
		hasmap.put("sex_man", list);
		hasmap.put("sex_she", list2);
		
		return hasmap;

	}
	
	//나이 20대
	@RequestMapping("/admin_chart/admin_Chart_Basicarea.do")
	public @ResponseBody Map<String, Object> chart3() {

		Map<String, Object> hasmap = new HashMap<String, Object>();
		List<ChartDto> list = null;
		list = service.Chart_age_20();
		hasmap.put("age_20", list);
		
		return hasmap;

	}
	
	//년도별 2014년
	@RequestMapping("/admin_chart/admin_Chart_Piechart.do")
	public @ResponseBody Map<String, Object> chart4() {

		Map<String, Object> hasmap = new HashMap<String, Object>();
		List<ChartDto> list = null;
		list = service.Chart_year_2014(); 
		hasmap.put("year_2014", list);
		
		return hasmap;

	}
	
  /* 통계 END : 김윤철--------------------------------------------------------------------- */
  
}
